#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>

#define STACK_SIZE 100

int contents[STACK_SIZE];
int top = 0;

void make_empty(void);
bool is_empty(void);
bool is_full(void);
void push(int);
int pop(void);
void stack_overflow(void);
void stack_underflow(void);

int main(void)
{
    char ch;
    int num1, num2;

    while (true)
    {
        printf("Enter an RPN expression: ");
        do
        {
            scanf(" %c", &ch);
            if (isdigit(ch))
            {
                push(ch - '0');
            }
            else if (ch == '*')
            {
                num1 = pop();
                num2 = pop();
                push(num2 * num1);
            }
            else if (ch == '+')
            {
                num1 = pop();
                num2 = pop();
                push(num2 + num1);
            }
            else if (ch == '/')
            {
                num1 = pop();
                num2 = pop();
                push(num2 / num1);
            }
            else if (ch == '-')
            {
                num1 = pop();
                num2 = pop();
                push(num2 - num1);
            }
            else if (ch == '=')
            {
                printf("Value of expression: %d\n", pop());
                break;
            }
            else
            {
                return 0;
            }
        } while (ch != '\n');
    }
}

void make_empty(void)
{
    top = 0;
}

bool is_empty(void)
{
    return top == 0;
}

bool is_full(void)
{
    return top == STACK_SIZE;
}

void push(int i)
{
    if (is_full())
        stack_overflow();
    else
        contents[top++] = i;
}

int pop(void)
{
    if (is_empty())
        stack_underflow();
    else
    {
        return contents[--top];
    }
}

void stack_overflow(void)
{
    printf("Expression is too complex\n");
    exit(EXIT_FAILURE);
}

void stack_underflow(void)
{
    printf("Not enough operands in expression\n");
    exit(EXIT_FAILURE);
}